package com.douziit.meilianoa.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.douziit.meilianoa.base.exception.TipException;
import com.douziit.meilianoa.base.request.PageRequest;
import com.douziit.meilianoa.base.response.ResponseData;
import com.douziit.meilianoa.base.response.ResponseUtil;
import com.douziit.meilianoa.entity.Account;
import com.douziit.meilianoa.entity.ApprovalLog;
import com.douziit.meilianoa.entity.Fees;
import com.douziit.meilianoa.entity.ProjectInfo;
import com.douziit.meilianoa.service.AccountService;
import com.douziit.meilianoa.service.ApprovalLogService;
import com.douziit.meilianoa.service.FeesService;
import com.douziit.meilianoa.service.ProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.rmi.dgc.Lease;
import java.util.Date;

/**
 * 预算费用控制类
 */

@RestController
@RequestMapping("/fees")
public class FeesController {

    @Autowired
    private FeesService feesService;

    @Autowired
    private ProjectInfoService projectInfoService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private ApprovalLogService approvalLogService;

    /**
     * 添加预算/报销费用
     * 参数 : 必须有  reiBugFlag : ( 0:预算 \ 1:报销标志 )
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/addBudRei")
    public ResponseData addBudRei(@RequestBody Fees fees){
        Account account = accountService.findUserInfoByToken();
        fees.setCreatorId(account.getId());
        feesService.save(fees);
        ProjectInfo projectInfo = new ProjectInfo();
        if(fees.getReiBugFlag().equals(0)){
            projectInfo.setStatus(1);
        }else if(fees.getReiBugFlag().equals(1)){
            projectInfo.setStatus(4);
        }
        projectInfo.setId(fees.getProjectId());
        projectInfoService.updateById(projectInfo);
        return ResponseUtil.jsonSuccess(0);
    }

    /**
     * 修改预算\报销费用
     */
    @PostMapping("/changeFees")
    public ResponseData changeAccount(@RequestBody Fees fees){

        ProjectInfo projectInfo = projectInfoService.getById(fees.getProjectId());
        if (projectInfo == null){
            throw new TipException("无此项目id");
        }
        Account account = accountService.findUserInfoByToken();
        if (!fees.getCreatorId().equals(account.getId())){
            throw new TipException("无修改权限");
        }
        feesService.updateById(fees);
        return ResponseUtil.jsonSuccess(0);
    }

    /**
     * 删除预算\报销
     */
    @PostMapping("/deleteFees")
    public ResponseData deleteFees(@RequestBody PageRequest req){
        feesService.removeById(req.getFeesId());
        return ResponseUtil.jsonSuccess(0);
    }

    /**
     * 指定项目预算\报销费用详情
     * rei_bug_flag   0:预算   1:报销
     */
    @PostMapping("/feesDetail")
    public ResponseData feesDetail(@RequestBody PageRequest req){
        Fees fees = feesService.getOne(new QueryWrapper<Fees>().eq("project_id",req.getProjectId()).eq("rei_bug_flag",req.getReiBugFlag()));
        return ResponseUtil.jsonSuccess(fees);
    }


    /**
     *预算\报销未通过
     * 参数:   id:项目id   budgetReason:预算未通过原因\reimbursementReason:报销未通过原因
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/notPassReiBug")
    public ResponseData notPassBudget(@RequestBody PageRequest req){

        ProjectInfo projectInfo = projectInfoService.getById(req.getProjectId());
        if(projectInfo.getStatus().equals(1)){
            projectInfo.setStatus(2);
            projectInfo.setBudgetReason(req.getBudgetReason());
        }else if(projectInfo.getStatus().equals(4)){
            projectInfo.setStatus(6);
            projectInfo.setReimbursementReason(req.getReimbursementReason());
        }
        projectInfoService.updateById(projectInfo);
        //添加审核日志信息
        Account account = accountService.findUserInfoByToken();
        ApprovalLog approvalLog=new ApprovalLog();
        approvalLog.setProjectId(projectInfo.getId());
        approvalLog.setStatus(0);
        approvalLog.setApprovalRole(account.getRole());
        approvalLog.setCheckDate(new Date());
        approvalLogService.save(approvalLog);
        return ResponseUtil.jsonSuccess(0);
    }

    /**
     *预算\报销通过
     * 参数:   id:项目id
     */
    @PostMapping("/passReiBug")
    public ResponseData passBudget(@RequestBody PageRequest req){

        ProjectInfo projectInfo = projectInfoService.getById(req.getProjectId());
        if(projectInfo.getStatus().equals(1)){
            projectInfo.setStatus(3);
        }else if(projectInfo.getStatus().equals(4)){
            projectInfo.setStatus(5);
        }
        projectInfoService.updateById(projectInfo);
        //添加审核日志信息
        Account account = accountService.findUserInfoByToken();
        ApprovalLog approvalLog=new ApprovalLog();
        approvalLog.setProjectId(projectInfo.getId());
        approvalLog.setStatus(1);
        approvalLog.setApprovalRole(account.getRole());
        approvalLog.setCheckDate(new Date());
        approvalLogService.save(approvalLog);
        return ResponseUtil.jsonSuccess(0);
    }

    /**
     * 重新申请预算/报销
     */
    @PostMapping("/reapply")
    public ResponseData reapply(@RequestBody Fees fees){

        Fees f = feesService.getById(fees.getId());
        ProjectInfo projectInfo = projectInfoService.getOne(new QueryWrapper<ProjectInfo>().eq("id",f.getProjectId()));
        if(f.getReiBugFlag().equals(0)){
            projectInfo.setStatus(1);
        }else if(f.getReiBugFlag().equals(1)){
            projectInfo.setStatus(4);
        }
        return ResponseUtil.jsonSuccess(0);
    }
}
